# -*- coding: utf-8 -*-
# @Time    : 2019/10/24 9:34
# @Author  : Damn7Kx
# @Software: PyCharm
from urllib.parse import urlparse
from NewsSpider.settings import *
import redis


class Redis_DB(object):

    def __init__(self):
        self.host = REDIS_HOST
        self.port = REDIS_PORT
        self.passwd = REDIS_PASSWORD
        self.db = REDIS_DB
        self.decode = DECODE_RESPONSES
        self.host2 = PROXY_REDIS_HOST
        self.port2 = PROXY_REDIS_PORT
        self.passwd2 = PROXY_REDIS_PASSWORD
        self.db2 = PROXY_REDIS_DB
        self.decode2 = PROXY_DECODE_RESPONSES
        pool = redis.ConnectionPool(host=self.host,
                                    port=self.port,
                                    password=self.passwd,
                                    db=self.db,
                                    decode_responses=self.decode)
        pool2 = redis.ConnectionPool(host=self.host2,
                                    port=self.port2,
                                    password=self.passwd2,
                                    db=self.db2,
                                    decode_responses=self.decode2)
        # 去重ip
        self.redis_db = redis.StrictRedis(connection_pool=pool)
        # 代理ip
        self.redis_db2 = redis.StrictRedis(connection_pool=pool2)

    @classmethod
    def random_member(cls):
        proxy = Redis_DB().redis_db2.srandmember("localproxyip", 1)
        return proxy

    # 去重
    @classmethod
    def check_exist(cls, key_name, key, value=''):
        # 返回值是1或0,已经存在则为0,不存在为1
        # result = self.db2.hsetnx(name, key, value)
        result = Redis_DB().redis_db.hsetnx(key_name, key, value)
        return result

    def get_ip_2(self):
        result = self.redis_db2.hgetall("localproxyip")
        ips = []
        for k, v in result.items():
            ip = k + ":" + v
            ips.append(ip)
        return ips

    # 服务器的文章去重
    def check_exist_2(self, name, key, value=''):
        # 返回值是1或0,已经存在则为0,不存在为1
        result = self.redis_db.hsetnx(name, key, value)
        return result


class Proxy_Redis(object):

    def __init__(self):
        self.host = PROXY_REDIS_HOST
        self.port = PROXY_REDIS_PORT
        self.passwd = PROXY_REDIS_PASSWORD
        self.db = PROXY_REDIS_DB
        self.decode = PROXY_DECODE_RESPONSES
        pool = redis.ConnectionPool(host=self.host,
                                    port=self.port,
                                    password=self.passwd,
                                    db=self.db,
                                    decode_responses=self.decode)
        self.redis_db = redis.StrictRedis(connection_pool=pool)

    def get_ip_2(self):
        result = self.redis_db.hgetall("testip")
        ips = []
        for k, v in result.items():
            ip = k + ":" + v
            ips.append(ip)
        return ips



if __name__ == '__main__':
    # print(Redis_DB.random_member())
    print(Redis_DB().check_exist_2('wenshuquchong','iOz2RxdWFeJGaHaHZpKJtA'))